package com.lch.designpattern.singleton.threadlocal;

/**
 * threadlocal的单例的模式    伪线程安全单例         注册试单例
 *
 * @author: liuchenhui
 * @create: 2019-04-17 22:17
 **/
// 使用ThreadLocal实现数据源动态切换
// 将spring源码的时候，手写orm框架并且实现多数据源动态切换
// 数据源路由
public class ThreadLocalSingleton {
    private ThreadLocalSingleton() {
    }

    private static final ThreadLocal<ThreadLocalSingleton> threadlocalInstance = new ThreadLocal<ThreadLocalSingleton>() {
        @Override
        protected ThreadLocalSingleton initialValue() {
            return new ThreadLocalSingleton();
        }
    };

    public static ThreadLocalSingleton getInstance() {
        return threadlocalInstance.get();
    }
}
